package postAPI

import (
	"github.com/gin-gonic/gin"
	"go-gin-init/app/models/req"
	"go-gin-init/app/service"
	"go-gin-init/common/ctx"
	"go-gin-init/common/result"
	"go-gin-init/common/xerr"
)

// PostDelete
// @Summary 删除帖子
// @Description
// @Tags POST
// @Produce json
// @Param id path int true "请求参数"
// @Success 200 {object} result.JsonResult "{"code":200,"data":{},"msg":"SUCCESS"}"
// @Failure 400 {object} result.JsonResult "{"code":200,"data":{},"msg":""}"
// @Failure 500 {string} string "Internal Server Error"
// @Router /post/del/{id} [delete]
func PostDelete(c *gin.Context) {

	var u req.PostDeleteReq
	if err := c.ShouldBindUri(&u); err != nil {
		result.ParamErrorResult(c, err)
		return
	}

	defaultPostModel := service.NewDefaultPostModel()

	post, err := defaultPostModel.FindById(u.Id)
	if err != nil {
		result.JsonRsp(c, nil, xerr.NewErrMsg("删除失败"))
		return
	}

	//	检测权限
	if err := ctx.CheckPermissionCtx(post.AuthorId, c); err != nil {
		result.JsonRsp(c, nil, err)
		return
	}

	err = defaultPostModel.DeletePost(u.Id)
	if err != nil {
		result.JsonRsp(c, nil, xerr.NewErrCode(xerr.SERVER_COMMON_ERROR))
		return
	}

	result.JsonRsp(c, nil, nil)
}
